package com.eduadmin.controller;

import com.eduadmin.entity.Page;
import com.eduadmin.entity.StatisticsFinished;
import com.eduadmin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StatisticsFinishedController {
    //构造函数自动装配
    private final IStatisticsFinishedService statisticsFinishedService;
    private final ISingleMaxService singleMaxService;
    private final IPassInformService passInformService;
    private final ITeamCourseAveService teamCourseAveService;
    private final ITeamCourseMedianService teamCourseMedianService;
    private final ITeamCourseRateService teamCourseRateService;
    private final ITotalDivideService totalDivideService;
    private final ICourseDivideService courseDivideService;
    @Autowired
    public StatisticsFinishedController(IStatisticsFinishedService statisticsFinishedService,
                                        ISingleMaxService singleMaxService,
                                        IPassInformService passInformService,
                                        ITeamCourseAveService teamCourseAveService,
                                        ITeamCourseMedianService teamCourseMedianService,
                                        ITeamCourseRateService teamCourseRateService,
                                        ITotalDivideService totalDivideService,
                                        ICourseDivideService courseDivideService){
        this.statisticsFinishedService = statisticsFinishedService;
        this.singleMaxService=singleMaxService;
        this.passInformService=passInformService;
        this.teamCourseAveService=teamCourseAveService;
        this.teamCourseMedianService=teamCourseMedianService;
        this.teamCourseRateService=teamCourseRateService;
        this.totalDivideService=totalDivideService;
        this.courseDivideService=courseDivideService;
    }
    // zlc 统计完成表（t_statistics_finished）：对已经统计了统计信息进行查询和删除。
    @RequestMapping(value = "getAllStatisticsFinishedData")
    @ResponseBody
    public List<StatisticsFinished> getAllStatisticsFinishedData( @RequestParam(value = "page", required = false) String page,
                                                                  @RequestParam(value = "rows", required = false) String rows){
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Integer OFFSET=pageBean.getOffset();
        Integer ROWS=pageBean.getRows();
        return statisticsFinishedService.getAllStatisticsFinished(OFFSET,ROWS);
    }
    // 根据所选（学期，年级，考试，分类）查询已经完成了统计的信息
    @RequestMapping(value = "/searchStatisticsFinishedData")
    @ResponseBody
    public Map<String,Object> searchStatisticsFinishedData(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            @RequestParam("semesterName") String semesterName,
            @RequestParam("gradeDatatableName") String gradeDatatableName,
            @RequestParam("examName") String examName,
            @RequestParam("categoryName") String categoryName){

        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> res = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();//查询参数map
        Integer OFFSET=pageBean.getOffset();
        Integer ROWS=pageBean.getRows();
        paraMap.put("offset",OFFSET);
        paraMap.put("rows",ROWS);
        paraMap.put("semesterName",semesterName);
        paraMap.put("gradeDatatableName",gradeDatatableName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);

        // 根据(学期，年级名称（本处用年级表名称），考试，分类)查询本年级已经完成统计的信息
        List<StatisticsFinished> statisticsFinishedList= new ArrayList<>();
        if (semesterName.length()<=0 || gradeDatatableName.length()<=0 ||
                examName.length()<=0 || categoryName.length()<=0) {
            statisticsFinishedList=statisticsFinishedService.getAllStatisticsFinished(OFFSET,ROWS);}
        else {
           statisticsFinishedList=statisticsFinishedService.getAllStatisticsFinishedByParams(paraMap);
        }
        int total = statisticsFinishedService.getAllStatisticsFinishedNumber();
        res.put("rows", statisticsFinishedList);
        res.put("total", total);
        return res;
    }
    // 删除统计完成表中选中的记录，及该记录对应的统计表中的所有满足条件的记录
    @RequestMapping(value = "deleteStatisticsFinishedData")
    @ResponseBody
    public Map<String,String>  deleteStatisticsFinishedData(@RequestParam String statisticsTableName,
                                                            @RequestParam String semesterName,
                                                            @RequestParam String gradeName,
                                                            @RequestParam String examName,
                                                            @RequestParam String categoryName
                                                            ){
        Map<String,Object> paraMap=new HashMap<>();
        paraMap.put("semesterName",semesterName);
        paraMap.put("gradeName",gradeName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);
        // 根据“统计表名”，删除对应统计表中符合条件的所有记录
        int n=0;
        switch(statisticsTableName){
            case "t_single_max":{
                n= singleMaxService.deleteByParams(paraMap);
                break;}
            case "t_pass_inform":{
                n=passInformService.deleteByParams(paraMap);
                break;}
            case "t_team_course_ave":{
                n= teamCourseAveService.deleteByParams(paraMap);
                break;}
            case "t_team_course_median":{
                 n= teamCourseMedianService.deleteByParams(paraMap);
                break;}
            case "t_team_course_rate":{
                 n = teamCourseRateService.deleteByParams(paraMap);
                break;}
            case "t_total_divide":{
                 n = totalDivideService.deleteByParams(paraMap);
                break;}
            case "t_course_divide":{
                n = courseDivideService.deleteByParams(paraMap);
                break;}
        }
        paraMap.put("statisticsTableName",statisticsTableName);
        // 最后删除statisticsFinished表中的一条记录。
        int m=0;
        m = statisticsFinishedService.deleteByParams(paraMap);

        Map<String,String> res=new HashMap<>();
        if (n != 0 && m !=0) {
            res.put("type", "success");
            res.put("msg", "删除成功！");
            return res;
        }
        res.put("type", "error");
        res.put("msg", "删除失败！");
        return res;
    }
}
